package com.gcxy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.conf.result.R;
import com.gcxy.dto.AccountInfoDto;
import com.gcxy.entity.AccountInfo;
import com.gcxy.service.AccountInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiang
 * @since 2023-12-12
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/accountInfo")
public class AccountInfoController {

    @Autowired
    private AccountInfoService accountInfoService;

    @ApiOperation("注册接口")
    @PostMapping("/addAccountInfo")
    public R addUser(@Validated @RequestBody AccountInfo accountInfo) {
        return accountInfoService.addAccountInfo(accountInfo);
    }

    @ApiOperation("分页查询用户接口")
    @PostMapping("/getAccountInfoByPage")
    public R<Page<AccountInfo>> getUserByPage(@RequestBody AccountInfoDto accountInfoDto) {
        return accountInfoService.getAccountInfoByPage(accountInfoDto);
    }
//
    @ApiOperation("修改用户信息接口")
    @PostMapping("updateAccountInfo")
    public R updateUser(@RequestBody AccountInfo accountInfo) {
        return accountInfoService.updateAccountInfo(accountInfo);
    }
//
//    @ApiOperation("删除用户信息接口")
//    @PostMapping("deleteAccountInfo")
//    public R delectUser(@RequestBody AccountInfo accountInfo) {
//        return accountInfoService.deleteAccountInfo(accountInfo);
//    }

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public R<Map<String, Object>> login(@RequestBody AccountInfo accountInfo) {
        // 根据账号(或者手机号，邮箱)密码  去数据库中查询，匹配成则登录成功
        Map<String, Object> data = accountInfoService.login(accountInfo);
        if (data != null) {
            return R.Success("用户登录成功", data);
        }
        return R.Failed(401, "用户登录失败");
    }

    @ApiOperation("获取用户信息接口")
    @GetMapping("/current")
    public R<Map<String, Object>> current(@RequestHeader("Token") String token) {
        // 根据token 获取用户信息
        Map<String, Object> data = accountInfoService.current(token);
        if (data != null) {
            return R.Success("获取用户信息成功", data);
        }
        return R.Failed(200001, "用户信息获取失败");
    }

    @ApiOperation("退出登录接口")
    @GetMapping("/logout")
    public R<Map<String, Object>> logout(@RequestHeader("Token") String token) {
        accountInfoService.logout(token);
        return R.Success();
    }

}

